<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-17 09:28
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class GetWxPortfolioListCriteria extends BaseCriterias
{
	protected $params;

	public function __construct($storeId = 0, $barberId = 0, $portfolioName = "",$memberId=0,$portfolio_id=null)
	{
		$this->params = [
			'user_id'      => $barberId,
			'store_id'     => $storeId,
			'portfolio_name' => $portfolioName,
			'member_id'    => $memberId,
			'portfolio_id' => $portfolio_id,
		];
	}

	public function apply($model, RepositoryInterface $repository)
	{
        $portfolio_id = $this->params['portfolio_id'];
		$portfolioName = '';
		$where=[];
		empty($this->params['user_id']) ?: $where['user_id'] = $this->params['user_id'];
		empty($this->params['store_id']) ?: $where['store_id'] = $this->params['store_id'];
		empty($this->params['portfolio_name']) ?: $portfolioName = $this->params['portfolio_name'];
		$memberId = $this->params['member_id'];
		return $model
			->with('store:id,name,address,doorplate,level', 'resource:id,portfolio_id,resource_id', 'user:id,nick,mobile,portrait')
			->when(!empty($portfolioName), function ($query) use ($portfolioName) {
				$query->where('name','like','%'.$portfolioName.'%');
			})
			->with('storeService:id,name,price,type')
//			->with(['orderItems'=>function($v){
//				$v->with(['order'=>function($v){
//					$v->select(['id','status'])->where(['status'=>2]);
//				}]);
//			}])
			->withCount('userPortfolioLike')
			->when($memberId,function ($v)use($memberId){
				$v->with(['userPortfolioLike' => function($s) use($memberId){
					$s->where('member_id','=',$memberId);
				}]);
			})
            ->when($portfolio_id,function ($query) use ($portfolio_id){
                $query->where('id',$portfolio_id);
//	                ->where('deleted_at','=','null');
            })
			->when(!empty($where),function ($v)use($where){
				$v->where($where);
			})
			->orderBy('id', 'desc');
	}


}